package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.ActivityDetailData;
import com.alm.token.ui.data.bean.ActivityIndexData;
import com.alm.token.ui.data.bean.ActivityJoinListData;
import com.alm.token.ui.data.bean.G12CanData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.UserListLHData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.ActivityView;
import com.alm.token.ui.view.QuantitativeOrderOpenAddView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.LanguageUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseViewHolder;
import com.haozhang.lib.SlantedTextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import baibeiwallet.Constant;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ActivityListActivity extends BaseActivity implements ActivityView<Object> {
    @BindView(R.id.leftItem)
    ImageView leftItem;
    @BindView(R.id.centerItem)
    TextView centerItem;
    @BindView(R.id.rightItem)
    TextView rightItem;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;

    private RefreshLoadMoreManager<ActivityIndexData.DataBean> manager;
    private List<ActivityIndexData.DataBean> dataBeans = new ArrayList<>();
    private int ActivityForResultCode = 1;

    private int limit = 100, page = 1;
    private boolean isfull = false;

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_activitelist;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this)
                .setTitle(getString(R.string.tv_hint_activity))
                .setRightText(getString(R.string.banner_hint_jilu), this);

        rightItem.setOnClickListener(this);
        initManager();
        swipeRefreshLayout.setVisibility(View.GONE);
        nodatashowBody.setVisibility(View.VISIBLE);
    }

    private void initManager() {
        ActivityListAdapter adapter = new ActivityListAdapter(this);
        manager = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
                        if (dataBeans != null && dataBeans.size() > position) {
                            Intent intent = new Intent(ActivityListActivity.this, ActivityDeatailActivity.class);
                            intent.putExtra("databean", dataBeans.get(position));
                            startActivityForResult(intent, ActivityForResultCode);
//                            startActivity(intent);
                        }
                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .build();
//                .setOpenLoadMore(true)
//                .setOnLoadMoreListener(new RefreshLoadMoreManager.OnLoadMoreListener() {
//                    @Override
//                    public void onLoadMore() {
//                        manager.getAdapter().loadMoreEnd(true);
//                        manager.getAdapter().loadMoreFail();
//                        showWaitDialog();
//                        preenter.getUserListLHData(String.valueOf(limit), String.valueOf(page));
//                    }
//                })
        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected void loadData() {
        limit = 100;
        page = 1;
        preenter.getActivityIndexData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rightItem:
                startActivity(ActivityRegistrationRecordActivity.class);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == ActivityForResultCode) {
//            if (resultCode == QuantitativeOrderOpenAddActivity.AddAppendSucc) {
////                showToast("添加成功，刷新列表");
//                loadData();
//            }
//            if (resultCode == QuantitativeOrderDetailActivity.AppendSucc) {
////                showToast("添加成功，刷新列表");
//                loadData();
//            }
//        }
    }

    @Override
    public void getData(Object o) {

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        }else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @Override
    public void getActivityIndexData(ActivityIndexData activityIndexData) {


        if (activityIndexData.getStatus() == 0 && activityIndexData.getData() != null) {
            dataBeans.clear();
            dataBeans.addAll(activityIndexData.getData());
            for (int i = 0; i < dataBeans.size(); i++) {
                dataBeans.get(i).setLayoutId(ActivityListAdapter.LAYOUT_ID);
            }
            manager.setData(dataBeans);
        } else {
            showToast(activityIndexData.getMsg());
        }

        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
    }

    @Override
    public void getActivityDetailData(ActivityDetailData activityDetailData) {

    }

    @Override
    public void getActivityJoinData(RegisterData registerData) {

    }

    @Override
    public void getActivityPayOrderData(RegisterData registerData) {

    }

    @Override
    public void getActivityJoinListData(ActivityJoinListData activityJoinListData) {

    }

    @Override
    public void getCode(RegisterData registerData) {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadData();
    }


    public class ActivityListAdapter extends BaseAdapter<ActivityIndexData.DataBean> {

        public static final int LAYOUT_ID = R.layout.item_activity_list;

        public ActivityListAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, ActivityIndexData.DataBean item, int itemType) {
            /**
             * id : 1
             * title : 特使
             * img : http://file.gufang.co/1e9d038c7e500a8f52bc4928956f521.png
             * detailImg : http://file.gufang.co/3a860ddfd773dffde2556039948f164.png
             * price : 15.0
             * moneyType : 1
             * moneyTypeName : ETH
             * moneyTypeImg : http://file.gufang.co/eth_img.png
             * country : 国家
             * city : 城市
             * address : 测试的地址
             * status : 1
             * startTime : 1560147843000
             * endTime : 1561271050000
             * createTime : 1559802255000
             * updateTime : 1560394565000
             * content : 活动内容
             * contentEn : activity content
             * readNum : 0
             */

            //设置图片圆角角度
//            RoundedCorners roundedCorners= new RoundedCorners(12);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
//            RequestOptions options=RequestOptions.bitmapTransform(roundedCorners) ;.apply(options)

            ImageView imageView = helper.getView(R.id.img_activitylistimg);
            GlideApp.with(context).load(item.getImg()).into(imageView);
            ImageView imgWalletView = helper.getView(R.id.img_activity_list_expend);
            GlideApp.with(context).load(item.getMoneyTypeImg()).into(imgWalletView);
            helper.setText(R.id.tv_activity_list_title, item.getTitle());
            helper.setText(R.id.tv_activity_list_time, CommonUtil.getDateTimeFromMillisecond1(item.getStartTime()) + "~" + CommonUtil.getDateTimeFromMillisecond1(item.getEndTime()));
            helper.setText(R.id.tv_activity_list_locial, item.getCountry() + item.getCity());
            helper.setText(R.id.tv_activity_list_staue, item.getStatus() == 1 ? getString(R.string.tv_hint_activity_open) : getString(R.string.tv_hint_activity_closd));// 状态 1 开启 0 关闭
//            helper.setText(R.id.tv_activity_list_expend, item.getPrice() + item.getMoneyTypeName());
            helper.setText(R.id.tv_activity_list_expend, item.getMoney() + item.getMoneyTypeName());
            helper.setTextColor(R.id.tv_activity_list_staue, getResources().getColor(item.getStatus() == 1 ? R.color.colorgreen : R.color.colorPrimary444444));
            helper.setImageDrawable(R.id.img_activity_list_staue, getResources().getDrawable(item.getStatus() == 1 ? R.drawable.icon_activity_list_openstaue : R.drawable.icon_activity_list_closestaue));
        }

    }
}
